﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using DevExpress.Web.Mvc;
using EnterpriseFund.Common;
using Newtonsoft.Json;
using DevExpress.Web.ASPxGridView;
using System.Web.UI;
using EnterpriseFund.BLL;
using DevExpress.XtraReports.UI;
using DevExpress.XtraPrinting;

namespace EnterpriseFund.Controllers
{
    public class QueryAllowanceController : Controller
    {
        //
        // GET: /QueryAllowance/
      
        public ActionResult Index()
        {
            EnterpriseFund.BLL.QueryAllowance QueryAllowanceInfobll = new EnterpriseFund.BLL.QueryAllowance();
            var zsxx = QueryAllowanceInfobll.GetModelList("");
            BLL.FinanceProp f = new BLL.FinanceProp();
            BLL.EnterpriseProp ps = new BLL.EnterpriseProp();
            var vf = f.GetAllList();
            var vp = ps.GetAllList();
            ViewData["FinancePropList"] = vf;
            ViewData["EnterprisePropList"] = vp;
            var vsbzsxx2 = vartodatatable(zsxx);
            //foreach (Model.QueryAllowance x in zsxx)
            //{
            //    x.FAuditAmount = decimal.Parse(x.FAuditAmount.ToString()) / 10000;
            //}
          //  return View("Index", vsbzsxx2);
            return View();
        }

        public string sty = "Glass";
        public ActionResult QueryAllowanceListPartial()
        {
          //  GetWhereString();
            var vsbzsxx = GetModelVSBZSXX();
            var vsbzsxx2 = vartodatatable(vsbzsxx);
       
            //foreach (var x in vsbzsxx2)
            //{
            //    x.FAuditAmount = decimal.Parse(x.FAuditAmount.ToString()) / 10000;
            //}
            return View(vsbzsxx2 );
        }
          #region 导出EXCEL
        public ActionResult ExportToXls()
        {
            //var NSRMC = Request["NSRMC"];
            //var FFinance = IsNull(ComboBoxExtension.GetValue<string>("FFinance1"), Request["FFinance"]);
            //var NSRBM = Request["NSRBM"];
            //var NSRSBH = Request["NSRSBH"];
            //var FNewEnterprise = IsNull(ComboBoxExtension.GetValue<string>("FNewEnterprise1"), Request["FNewEnterprise"]);
            //var FIssueID = Request["FIssueID"];
            //var ISPAY = Request["ISPAY"];
            //var FYear = Request["FYear"];
            //var BTJEA = Request["BTJEA"];
            //var BTJEB = Request["BTJEB"];
            //var FApplyDate = Request["FApplyDate"];
            //var LastPayDate = Request["LastPayDate"];
            //var ISBASE = Request["ISBASE"];
            //var FFileNo = Request["FFileNo"];
            //var SKSJA = Request["SKSJA"];
            //var SKSJB = Request["SKSJB"];
            var NSRMC = Fetch.Post("hNSRMC");
   var FFinance= Fetch.Post("hFFinance");
      var  NSRBM=    Fetch.Post("hNSRBM");
     var   NSRSBH=     Fetch.Post("hNSRSBH"); 
      var      FNewEnterprise=                      Fetch.Post("hFNewEnterprise");
       var  FIssueID=  Fetch.Post("hFIssueID");
      var  ISPAY=    Fetch.Post("hISPAY");
      var  FYear=     Fetch.Post("hFYear");
      var   BTJEA=     Fetch.Post("hBTJEA");
      var BTJEB=    Fetch.Post("hBTJEB");
     var   FApplyDate=    Fetch.Post("hFApplyDate");
     var   LastPayDate=     Fetch.Post("hLastPayDate");
     var   ISBASE=      Fetch.Post("hISBASE");
   var  FFileNo=      Fetch.Post("hFFileNo");
    var    SKSJA=    Fetch.Post("hSKSJA");
     var  SKSJB =      Fetch.Post("hSKSJB");
            ViewBag.NSRMC = NSRMC;
            ViewBag.FFinance = FFinance;
            ViewBag.NSRBM = NSRBM;
            ViewBag.NSRSBH = NSRSBH;
            ViewBag.FNewEnterprise = FNewEnterprise;
            ViewBag.FIssueID = FIssueID;
            ViewBag.ISPAY = ISPAY;
            ViewBag.FYear = FYear;
            ViewBag.BTJEA = BTJEA;
            ViewBag.BTJEB = BTJEB;
            ViewBag.FApplyDate = FApplyDate;
            ViewBag.LastPayDate = LastPayDate;
            ViewBag.ISBASE = ISBASE;
            ViewBag.FFileNo = FFileNo;
            ViewBag.SKSJA = SKSJA;
            ViewBag.SKSJB = SKSJB;
            //ViewBag.FAmount1 = FAmount1;
            //ViewBag.FAmount2 = FAmount2;
            //ViewBag.FFileNo = FFileNo;
            //    //   ViewBag.FNote = FNote;
            //    //   ViewBag.FPayDateBegin = FPayDateBegin;
            //    //   ViewBag.FPayDateEnd = FPayDateEnd;
            //     //  ViewBag.nsrbm = nsrbm;
            //       ViewBag.nsrmc = nsrmc;
            //       ViewBag.nKHYH = nKHYH;
            //       ViewBag.FFinance = FFinance;
            var filter = "1=1";
            if (!string.IsNullOrEmpty(ISPAY) && ISPAY == "true")
                filter += string.Format("and NeedAmount =0 ");

            if (!string.IsNullOrEmpty(ISBASE) && ISBASE == "true")
                filter += string.Format("and FBaseAmount >0 ");
            //if (!string.IsNullOrEmpty(SFBF) && SFBF == "false")
            //    filter += string.Format("and FAuditAmount > FAmount ");
            //if (!string.IsNullOrEmpty(nsrbm))
            //    filter += string.Format("and NSRBM like '%{0}%'", nsrbm);
            if (!string.IsNullOrEmpty(NSRMC))
                filter += string.Format("and NSRMC like '%{0}%' ", NSRMC);
            if (!string.IsNullOrEmpty(NSRBM))
                filter += string.Format("and NSRBM like '%{0}%' ", NSRBM);
            if (!string.IsNullOrEmpty(NSRSBH))
                filter += string.Format("and NSRSBH like '%{0}%' ", NSRSBH);
            if (!string.IsNullOrEmpty(FIssueID))
                filter += string.Format("and FIssueID like '%{0}%' ", FIssueID);
            if (!string.IsNullOrEmpty(FYear))
                filter += string.Format("and FYear = '{0}' ", FYear);
            if (!string.IsNullOrEmpty(FFinance))
                filter += string.Format("and FFinancePropNo = '{0}' ", FFinance);
            if (!string.IsNullOrEmpty(FNewEnterprise))
                filter += string.Format("and FNewEnterprisePropNo= '{0}' ", FNewEnterprise);

            if (!string.IsNullOrEmpty(FFileNo))
            {
                filter += string.Format("and FFileNo like '%{0}%'", FFileNo);
            }
            if (!string.IsNullOrEmpty(BTJEA) & !string.IsNullOrEmpty(BTJEB))
            {
                filter += string.Format(" and FAuditAmount > '{0}'and FAuditAmount < '{1}' ", BTJEA, BTJEB);
            } 
            if (!string.IsNullOrEmpty(SKSJA) & !string.IsNullOrEmpty(SKSJB))
            {
                filter += string.Format(" and se > '{0}'and se < '{1}' ", SKSJA, SKSJB);
            }
            if (!string.IsNullOrEmpty(BTJEA))
            {
                filter += string.Format(" and FAuditAmount > '{0}'", BTJEA);
            }
            if (!string.IsNullOrEmpty(BTJEB))
            {
                filter += string.Format(" and FAuditAmount < '{0}' ", BTJEB);
            }

            if (!string.IsNullOrEmpty(SKSJA))
            {
                filter += string.Format(" and se > '{0}'", SKSJA);
            }
            if (!string.IsNullOrEmpty(SKSJB))
            {
                filter += string.Format(" and se < '{0}'", SKSJB);
            }
           
            //if (!string.IsNullOrEmpty(FAmount))
            //{
            //    filter += string.Format(" and FAmount = '{0}' ", FAmount);
            //}
            //if (FAmount1 != "" && FAmount2 != "")
            //{
            //    filter += " and FAmount >=" + FAmount1 + " and FAmount<=" + FAmount2 + " ";
            //}
            //else if (FAmount1 != "")
            //{
            //    filter += " and FAmount >=" + FAmount1;
            //}
            //else if (FAmount2 != "")
            //{
            //    filter += " and FAmount <=" + FAmount2;
            //}
            //if (!string.IsNullOrEmpty(FFileNo))
            //{
            //    filter += string.Format(" and FFileNo = '{0}' ", FFileNo);
            //}

            if (!string.IsNullOrEmpty(FApplyDate) || !string.IsNullOrEmpty(LastPayDate))
            {
                var begin = string.IsNullOrEmpty(FApplyDate) ? DateTime.MinValue.ToString() : FApplyDate;
                var end = string.IsNullOrEmpty(LastPayDate) ? DateTime.MaxValue.ToString() : LastPayDate;
                filter += string.Format(" AND FApplyDate <= '{0}'", end);
                if (!string.IsNullOrEmpty(FApplyDate))
                    filter += string.Format(" and FApplyDate >= '{0}'", begin);
            }
                   EnterpriseFund.BLL.QueryAllowance QueryAllowanceInfobll = new EnterpriseFund.BLL.QueryAllowance();
            var vsbzsxx= QueryAllowanceInfobll.GetModelList(filter);
            var table = vartodatatable(vsbzsxx);
           // return filter;
             return GridViewExtension.ExportToXls(ExportGridViewSettings, table, HttpUtility.UrlEncode("补贴信息查询导出.xls"), true);
        }
        public ActionResult ReportViewerPartial()
        {

            var vsbzsxx = GetModelVSBZSXX();
            DataTable dt = vartodatatable(vsbzsxx);
            MVCReportGeneratonHelper generator = new MVCReportGeneratonHelper();
            generator.CustomizeColumnsCollection += new CustomizeColumnsCollectionEventHandler(generator_CustomizeColumnsCollection);
            generator.CustomizeColumn += new CustomizeColumnEventHandler(generator_CustomizeColumn);
            XtraReport report = generator.GenerateMVCReport(OperateSession.GetFromCache("QueryAllowance") as MVCxGridViewState, dt);
            //  XtraReport report = generator.GenerateMVCReport(gridViewState, dt);
           // report.ReportPrintOptions.DetailCount = 2;
            //report.PaperKind = System.Drawing.Printing.PaperKind.A4;
          //   report.Bands[BandKind.Detail].Band.RootReport.PrintingSystem.
            this.ViewData["rpt"] = report;
            
            return PartialView("ReportViewerPartial");

        }
        public ActionResult Print()
        {
            //var NSRMC = Request["NSRMC"];
            //var FFinance = IsNull(ComboBoxExtension.GetValue<string>("FFinance1"), Request["FFinance"]);
            //var NSRBM = Request["NSRBM"];
            //var NSRSBH = Request["NSRSBH"];
            //var FNewEnterprise = IsNull(ComboBoxExtension.GetValue<string>("FNewEnterprise1"), Request["FNewEnterprise"]);
            //var FIssueID = Request["FIssueID"];
            //var ISPAY = Request["ISPAY"];
            //var FYear = Request["FYear"];
            //var BTJEA = Request["BTJEA"];
            //var BTJEB = Request["BTJEB"];
            //var FApplyDate = Request["FApplyDate"];
            //var LastPayDate = Request["LastPayDate"];
            //var ISBASE = Request["ISBASE"];
            //var FFileNo = Request["FFileNo"];
            //var SKSJA = Request["SKSJA"];
            //var SKSJB = Request["SKSJB"];
            var NSRMC = Fetch.Post("hNSRMC");
            var FFinance = Fetch.Post("hFFinance");
            var NSRBM = Fetch.Post("hNSRBM");
            var NSRSBH = Fetch.Post("hNSRSBH");
            var FNewEnterprise = Fetch.Post("hFNewEnterprise");
            var FIssueID = Fetch.Post("hFIssueID");
            var ISPAY = Fetch.Post("hISPAY");
            var FYear = Fetch.Post("hFYear");
            var BTJEA = Fetch.Post("hBTJEA");
            var BTJEB = Fetch.Post("hBTJEB");
            var FApplyDate = Fetch.Post("hFApplyDate");
            var LastPayDate = Fetch.Post("hLastPayDate");
            var ISBASE = Fetch.Post("hISBASE");
            var FFileNo = Fetch.Post("hFFileNo");
            var SKSJA = Fetch.Post("hSKSJA");
            var SKSJB = Fetch.Post("hSKSJB");
            ViewData["NSRMC"] = NSRMC;
            ViewData["FFinance"] = FFinance;
            ViewData["NSRBM"] = NSRBM;
            ViewData["NSRSBH"] = NSRSBH;
            ViewData["FNewEnterprise"] = FNewEnterprise;
            ViewData["FIssueID"] = FIssueID;
            ViewData["ISPAY"] = ISPAY;
            ViewData["FYear"] = FYear;
            ViewData["BTJEA"] = BTJEA;
            ViewData["BTJEB"] = BTJEB;
            ViewData["FApplyDate"] = FApplyDate;
            ViewData["LastPayDate"] = LastPayDate;
            ViewData["ISBASE"] = ISBASE;
            ViewData["FFileNo"] = FFileNo;
            ViewData["SKSJA"] = SKSJA;
            ViewData["SKSJB"] = SKSJB;
            //ViewBag.FAmount1 = FAmount1;
            //ViewBag.FAmount2 = FAmount2;
            //ViewBag.FFileNo = FFileNo;
            //    //   ViewBag.FNote = FNote;
            //    //   ViewBag.FPayDateBegin = FPayDateBegin;
            //    //   ViewBag.FPayDateEnd = FPayDateEnd;
            //     //  ViewBag.nsrbm = nsrbm;
            //       ViewBag.nsrmc = nsrmc;
            //       ViewBag.nKHYH = nKHYH;
            //       ViewBag.FFinance = FFinance;
            var filter = "1=1";
            if (!string.IsNullOrEmpty(ISPAY) && ISPAY == "true")
                filter += string.Format("and NeedAmount =0 ");

            if (!string.IsNullOrEmpty(ISBASE) && ISBASE == "true")
                filter += string.Format("and FBaseAmount >0 ");
            //if (!string.IsNullOrEmpty(SFBF) && SFBF == "false")
            //    filter += string.Format("and FAuditAmount > FAmount ");
            //if (!string.IsNullOrEmpty(nsrbm))
            //    filter += string.Format("and NSRBM like '%{0}%'", nsrbm);
            if (!string.IsNullOrEmpty(NSRMC))
                filter += string.Format("and NSRMC like '%{0}%' ", NSRMC);
            if (!string.IsNullOrEmpty(NSRBM))
                filter += string.Format("and NSRBM like '%{0}%' ", NSRBM);
            if (!string.IsNullOrEmpty(NSRSBH))
                filter += string.Format("and NSRSBH like '%{0}%' ", NSRSBH);
            if (!string.IsNullOrEmpty(FIssueID))
                filter += string.Format("and FIssueID like '%{0}%' ", FIssueID);
            if (!string.IsNullOrEmpty(FYear))
                filter += string.Format("and FYear = '{0}' ", FYear);
            if (!string.IsNullOrEmpty(FFinance))
                filter += string.Format("and FFinancePropNo = '{0}' ", FFinance);
            if (!string.IsNullOrEmpty(FNewEnterprise))
                filter += string.Format("and FNewEnterprisePropNo= '{0}' ", FNewEnterprise);

            if (!string.IsNullOrEmpty(FFileNo))
            {
                filter += string.Format("and FFileNo like '%{0}%'", FFileNo);
            }
            if (!string.IsNullOrEmpty(BTJEA) & !string.IsNullOrEmpty(BTJEB))
            {
                filter += string.Format(" and FAuditAmount > '{0}'and FAuditAmount < '{1}' ", BTJEA, BTJEB);
            }
            if (!string.IsNullOrEmpty(SKSJA) & !string.IsNullOrEmpty(SKSJB))
            {
                filter += string.Format(" and se > '{0}'and se < '{1}' ", SKSJA, SKSJB);
            }
            if (!string.IsNullOrEmpty(BTJEA))
            {
                filter += string.Format(" and FAuditAmount > '{0}'", BTJEA);
            }
            if (!string.IsNullOrEmpty(BTJEB))
            {
                filter += string.Format(" and FAuditAmount < '{0}' ", BTJEB);
            }

            if (!string.IsNullOrEmpty(SKSJA))
            {
                filter += string.Format(" and se > '{0}'", SKSJA);
            }
            if (!string.IsNullOrEmpty(SKSJB))
            {
                filter += string.Format(" and se < '{0}'", SKSJB);
            }

            //if (!string.IsNullOrEmpty(FAmount))
            //{
            //    filter += string.Format(" and FAmount = '{0}' ", FAmount);
            //}
            //if (FAmount1 != "" && FAmount2 != "")
            //{
            //    filter += " and FAmount >=" + FAmount1 + " and FAmount<=" + FAmount2 + " ";
            //}
            //else if (FAmount1 != "")
            //{
            //    filter += " and FAmount >=" + FAmount1;
            //}
            //else if (FAmount2 != "")
            //{
            //    filter += " and FAmount <=" + FAmount2;
            //}
            //if (!string.IsNullOrEmpty(FFileNo))
            //{
            //    filter += string.Format(" and FFileNo = '{0}' ", FFileNo);
            //}

            if (!string.IsNullOrEmpty(FApplyDate) || !string.IsNullOrEmpty(LastPayDate))
            {
                var begin = string.IsNullOrEmpty(FApplyDate) ? DateTime.MinValue.ToString() : FApplyDate;
                var end = string.IsNullOrEmpty(LastPayDate) ? DateTime.MaxValue.ToString() : LastPayDate;
                filter += string.Format(" AND FApplyDate <= '{0}'", end);
                if (!string.IsNullOrEmpty(FApplyDate))
                    filter += string.Format(" and FApplyDate >= '{0}'", begin);
            }
            EnterpriseFund.BLL.QueryAllowance QueryAllowanceInfobll = new EnterpriseFund.BLL.QueryAllowance();
            // var vsbzsxx = QueryAllowanceInfobll.GetModelList(filter);
            var vsbzsxx = QueryAllowanceInfobll.GetModelList(filter);
            DataTable dt = vartodatatable(vsbzsxx);
            #region getempname
            string EmpName = "";
            string DeptName = "";
            string loginName = OperateSession.get(GlobalConstant.PARAM_USERNAME);
            //获取部门信息
            string emplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            string entityNo = GlobalConstant.ENTITYNO;
            if (emplNo != "" && entityNo != "")
            {
                BLL.HREmpl hREmplBLL = new BLL.HREmpl();
                DataSet ds = hREmplBLL.GetEmplFromViewByEmplNo(emplNo, entityNo);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataTable dts = ds.Tables[0];
                    for (int i = 0; i < dts.Rows.Count; i++)
                    {
                        if (dts.Rows[i]["FIsMainPosition"].ToString() == "Y")
                        {
                            EmpName = dts.Rows[i]["FEmpName"].ToString();
                            DeptName = dts.Rows[i]["FDeptName"].ToString();
                            // OperateSession.add(GlobalConstant.PARAM_DEPTNO, dt.Rows[i]["FDeptNo"].ToString());
                            break;
                        }

                    }
                }
            }
            #endregion
               //  MVCxGridViewState gridViewState = new MVCxGridViewState(OperateSession.GetFromCache("QueryAllowance") as MVCxGridView);
            MVCReportGeneratonHelper generator = new MVCReportGeneratonHelper();
            generator.CustomizeColumnsCollection += new CustomizeColumnsCollectionEventHandler(generator_CustomizeColumnsCollection);
            generator.CustomizeColumn += new CustomizeColumnEventHandler(generator_CustomizeColumn);
            XtraReport report = generator.GenerateMVCReport(OperateSession.GetFromCache("QueryAllowance") as MVCxGridViewState, dt);
            //  XtraReport report = generator.GenerateMVCReport(gridViewState, dt);

            //report.PaperKind = System.Drawing.Printing.PaperKind.A4;


            this.ViewData["rpt"] = report;
              return View("Print", report);
            
        }

        private static DataTable vartodatatable(List<Model.QueryAllowance> vsbzsxx)
        {
            var zsxx = from p in vsbzsxx
                       group p by new { p.NSRDZDAH, p.NSRBM, p.NSRMC, p.NSRSBH, p.FYear, p.FFinancePropName,p.FIssueID,p.FFileNo } into g
                       select new
                       {
                           NSRDZDAH = g.Key.NSRDZDAH,

                           NSRBM = g.Key.NSRBM,
                           NSRMC = g.Key.NSRMC,
                           NSRSBH = g.Key.NSRSBH,
                           FAuditAmount = (g.Sum(gs => gs.FAuditAmount) / 10000).ToString("f2"),
                           FFinancePropName = g.Key.FFinancePropName,
                           //  FApplyDate = g.Where(h=>h.FApplyDate!=null).Min(sg => DateTime.Parse(sg.FApplyDate.ToString())).ToString("yyyy-MM-dd"),
                           FIssueID = g.Key.FIssueID,
                           FFileNo = g.Key.FFileNo,
                           FApplyDate = g.Where(h => h.FApplyDate != null).Count() > 0 ? g.Where(h => h.FApplyDate != null).Min(sg => DateTime.Parse(sg.FApplyDate.ToString())).ToString("yyyy-MM-dd") : "",
                           FYear = g.Key.FYear.ToString()
                       };
            //  MVCxGridView    gv=      Fetch.Post("gvEditing") as MVCxGridView;
            // return filter;
            //GridView  gv=          new GridViewExtension(exportGridViewSettings);
            //       gv.Bind(table).
            // EnterpriseFund.BLL.QueryAllowance QueryAllowanceInfobll = new EnterpriseFund.BLL.QueryAllowance();

            DataTable dt = new DataTable();
            if (dt.Columns.Count == 0)
            {
                dt.Columns.Add("NSRDZDAH");
                dt.Columns.Add("NSRBM");
                dt.Columns.Add("NSRSBH");
                dt.Columns.Add("NSRMC");
                dt.Columns.Add("FAuditAmount");
                dt.Columns.Add("FYear");
                dt.Columns.Add("FApplyDate");
                dt.Columns.Add("FFinancePropName");
                dt.Columns.Add("FIssueID");
                dt.Columns.Add("FFileNo");
            }
            foreach (var v in zsxx)
            {
                DataRow r = dt.NewRow();
                r["NSRBM"] = v.NSRBM;
                r["NSRSBH"] = v.NSRSBH;
                r["NSRMC"] = v.NSRMC;
                r["NSRDZDAH"] = v.NSRDZDAH;
                r["FAuditAmount"] = v.FAuditAmount;
                r["FYear"] = v.FYear;
                r["FApplyDate"] = v.FApplyDate;
                r["FIssueID"] = v.FIssueID;
                r["FFinancePropName"] = v.FFinancePropName;
                r["FFileNo"] = v.FFileNo;
                dt.Rows.Add(r);
            }
            return dt;
        }
        void generator_CustomizeColumn(object source, ControlCustomizationEventArgs e)
        {
            //if (e.FieldName == "Discontinued")
            //{
            //    XRShape control = new XRShape();
            //    control.SizeF = e.Owner.SizeF;
            //    control.LocationF = new System.Drawing.PointF(0, 0);
            //    e.Owner.Controls.Add(control);
            //    control.Shape = new ShapeStar()
            //    {
            //        StarPointCount = 5,
            //        Concavity = 30
            //    };
            //    control.BeforePrint += new System.Drawing.Printing.PrintEventHandler(control_BeforePrint);
            //    e.IsModified = true;
            //} 
            if (e.FieldName == "FAuditAmount")
            {
                //XRShape control = new XRShape();
                //control.SizeF = e.Owner.SizeF;
                //control.LocationF = new System.Drawing.PointF(0, 0);
                //e.Owner.Controls.Add(control);
                //control.Shape = new ShapeStar()
                //{
                //    StarPointCount = 5,
                //    Concavity = 30
                //};
                //control.BeforePrint += new System.Drawing.Printing.PrintEventHandler(control_BeforePrint);
                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleRight;
                //  e.IsModified = true;
            }
            if (e.FieldName == "FYear")
            {
                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleCenter;
            }

            if (e.FieldName == "NSRSBH")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleLeft;

            }
            if (e.FieldName == "NSRMC")
            {

                XRTableCell c = e.Owner as XRTableCell;
                c.TextAlignment = TextAlignment.MiddleLeft;

            }
        }
        

        void generator_CustomizeColumnsCollection(object source, ColumnsCreationEventArgs e)
        {
            for (int i = 0; i < e.ColumnsInfo.Count; i++)
            {


                if (e.ColumnsInfo[i].FieldName == "NSRBM")
                {
                    e.ColumnsInfo[i].ColumnWidth = 100;
                }
                if (e.ColumnsInfo[i].FieldName == "NSRSBH")
                {
                    e.ColumnsInfo[i].ColumnWidth = 170;
                }
                if (e.ColumnsInfo[i].FieldName == "NSRMC")
                {
                    e.ColumnsInfo[i].ColumnWidth = 320;
                }
                if (e.ColumnsInfo[i].FieldName == "FAuditAmount")
                {
                    e.ColumnsInfo[i].ColumnWidth = 140;
                    if (e.ColumnsInfo[i].FieldName == "FYear")
                    {
                        e.ColumnsInfo[i].ColumnWidth = 60;

                    }
                    if (e.ColumnsInfo[i].FieldName == "FApplyDate")
                    {
                        e.ColumnsInfo[i].ColumnWidth = 120;

                    }



                }



            }


            // e.ColumnsInfo[e.ColumnsInfo.Count - 1].IsVisible = false;
        }
        static GridViewSettings exportGridViewSettings;
        public static GridViewSettings ExportGridViewSettings
        {

            get
            {

                if (exportGridViewSettings == null)

                    exportGridViewSettings = CreateExportGridViewSettings();
                return exportGridViewSettings;
            }
        }
        private string IsNull(string s1, string s2)
        {
            return string.IsNullOrEmpty(s1) ? s2 : s1;
        }
        //public string ClearViewBag()
        //{
        //    try
        //    {
        //        ViewBag=null;
        //        return "OK";
        //    }
        //    catch (System.Exception ex)
        //    {
        //        return "error";
        //    }
        //}
        public string GetWhereString()
        {
            //绑定值

            //var FTaxClassAllowanceName = IsNull(ComboBoxExtension.GetValue<string>("FTaxClassAllowanceName"), Request["FTaxClassAllowanceName"]);
          //  var nKHYH = Request["NKHYH"];
           var   NSRMC=   Request["NSRMC"];
           var FFinance = IsNull(ComboBoxExtension.GetValue<string>("FFinance1"), Request["FFinance"]);
      var  NSRBM=   Request["NSRBM"];
     var   NSRSBH=    Request["NSRSBH"]; 
      var      FNewEnterprise=          IsNull(ComboBoxExtension.GetValue<string>("FNewEnterprise1"),           Request["FNewEnterprise"]);
       var  FIssueID= Request["FIssueID"];
      var  ISPAY=   Request["ISPAY"];
      var  FYear=    Request["FYear"];
      var   BTJEA=    Request["BTJEA"];
      var BTJEB=   Request["BTJEB"];
     var   FApplyDate=   Request["FApplyDate"];
     var   LastPayDate=    Request["LastPayDate"];
     var   ISBASE=     Request["ISBASE"];
   var  FFileNo=     Request["FFileNo"];
    var    SKSJA=   Request["SKSJA"];
     var  SKSJB =     Request["SKSJB"];
     ViewBag.NSRMC = NSRMC;
     ViewBag.FFinance = FFinance;
     ViewBag.NSRBM = NSRBM;
     ViewBag.NSRSBH = NSRSBH;
     ViewBag.FNewEnterprise = FNewEnterprise;
     ViewBag.FIssueID = FIssueID;
     ViewBag.ISPAY = ISPAY;
     ViewBag.FYear = FYear;
     ViewBag.BTJEA = BTJEA;
     ViewBag.BTJEB = BTJEB;
     ViewBag.FApplyDate = FApplyDate;
     ViewBag.LastPayDate = LastPayDate;
     ViewBag.ISBASE = ISBASE;
     ViewBag.FFileNo = FFileNo;
     ViewBag.SKSJA = SKSJA;
     ViewBag.SKSJB = SKSJB;
            //ViewBag.FAmount1 = FAmount1;
            //ViewBag.FAmount2 = FAmount2;
     //ViewBag.FFileNo = FFileNo;
     //    //   ViewBag.FNote = FNote;
     //    //   ViewBag.FPayDateBegin = FPayDateBegin;
     //    //   ViewBag.FPayDateEnd = FPayDateEnd;
     //     //  ViewBag.nsrbm = nsrbm;
     //       ViewBag.nsrmc = nsrmc;
     //       ViewBag.nKHYH = nKHYH;
     //       ViewBag.FFinance = FFinance;
            var filter = "1=1";
            if (!string.IsNullOrEmpty(ISPAY) && ISPAY == "true")
                filter += string.Format("and NeedAmount =0 ");

            if (!string.IsNullOrEmpty(ISBASE) && ISBASE == "true")
                filter += string.Format("and FBaseAmount >0 ");
            //if (!string.IsNullOrEmpty(SFBF) && SFBF == "false")
            //    filter += string.Format("and FAuditAmount > FAmount ");
            //if (!string.IsNullOrEmpty(nsrbm))
            //    filter += string.Format("and NSRBM like '%{0}%'", nsrbm);
            if (!string.IsNullOrEmpty(NSRMC))
                filter += string.Format("and NSRMC like '%{0}%' ", NSRMC);
            if (!string.IsNullOrEmpty(NSRBM))
                filter += string.Format("and NSRBM like '%{0}%' ", NSRBM);
            if (!string.IsNullOrEmpty(NSRSBH))
                filter += string.Format("and NSRSBH like '%{0}%' ", NSRSBH);
            if (!string.IsNullOrEmpty(FIssueID))
                filter += string.Format("and FIssueID like '%{0}%' ", FIssueID);
            if (!string.IsNullOrEmpty(FYear))
                filter += string.Format("and FYear = '{0}' ", FYear);
            if (!string.IsNullOrEmpty(FFinance))
                filter += string.Format("and FFinancePropNo = '{0}' ", FFinance);
            if (!string.IsNullOrEmpty(FNewEnterprise))
                filter += string.Format("and FNewEnterprisePropNo= '{0}' ", FNewEnterprise);

            if (!string.IsNullOrEmpty(FFileNo))
                filter += string.Format("and FFileNo like '%{0}%'  escape '\\'", FFileNo.Replace("[", "\\[").Replace("]", "\\]"));
            if (!string.IsNullOrEmpty(BTJEA) & !string.IsNullOrEmpty(BTJEB))
            {
                filter += string.Format(" and FAuditAmount > '{0}'and FAuditAmount < '{1}' ", BTJEA, BTJEB);
            }
            if (!string.IsNullOrEmpty(SKSJA) & !string.IsNullOrEmpty(SKSJB))
            {
                filter += string.Format(" and se > '{0}'and se < '{1}' ", SKSJA, SKSJB);
            }
            if (!string.IsNullOrEmpty(BTJEA))
            {
                filter += string.Format(" and FAuditAmount > '{0}'", BTJEA);
            }
            if (!string.IsNullOrEmpty(BTJEB))
            {
                filter += string.Format(" and FAuditAmount < '{0}' ", BTJEB);
            }

            if (!string.IsNullOrEmpty(SKSJA))
            {
                filter += string.Format(" and se > '{0}'", SKSJA);
            }
            if (!string.IsNullOrEmpty(SKSJB))
            {
                filter += string.Format(" and se < '{0}'", SKSJB);
            }
            //if (!string.IsNullOrEmpty(FAmount))
            //{
            //    filter += string.Format(" and FAmount = '{0}' ", FAmount);
            //}
            //if (FAmount1 != "" && FAmount2 != "")
            //{
            //    filter += " and FAmount >=" + FAmount1 + " and FAmount<=" + FAmount2 + " ";
            //}
            //else if (FAmount1 != "")
            //{
            //    filter += " and FAmount >=" + FAmount1;
            //}
            //else if (FAmount2 != "")
            //{
            //    filter += " and FAmount <=" + FAmount2;
            //}
            //if (!string.IsNullOrEmpty(FFileNo))
            //{
            //    filter += string.Format(" and FFileNo = '{0}' ", FFileNo);
            //}

            if (!string.IsNullOrEmpty(FApplyDate) || !string.IsNullOrEmpty(LastPayDate))
            {
                var begin = string.IsNullOrEmpty(FApplyDate) ? DateTime.MinValue.ToString() : FApplyDate;
                var end = string.IsNullOrEmpty(LastPayDate) ? DateTime.MaxValue.ToString() : LastPayDate;
                filter += string.Format(" AND FApplyDate <= '{0}'", end);
                if (!string.IsNullOrEmpty(FApplyDate))
                    filter += string.Format(" and FApplyDate >= '{0}'", begin);
            }

            return filter;

        }
        public List<Model.QueryAllowance> GetModelVSBZSXX()
        {
            var strWhere = GetWhereString();
            EnterpriseFund.BLL.QueryAllowance QueryAllowanceInfobll = new EnterpriseFund.BLL.QueryAllowance();
            var zsxx = QueryAllowanceInfobll.GetModelList(strWhere);
            return zsxx;
        }
        public static GridViewSettings CreateExportGridViewSettings()
        {
            var setting = new GridViewSettings();
            setting.Name = "gvEditing";
            setting.KeyFieldName = "NSRDZDAH";
            setting.CallbackRouteValues = new { Controller = "QueryAllowance", Action = "QueryAllowanceListPartial" };
            setting.Width = System.Web.UI.WebControls.Unit.Percentage(100);
            setting.SettingsPager.PageSize = EnterpriseFund.Common.GlobalConstant.CONSTRING_PAGESIZE;
           setting.Columns.Add("NSRBM", "纳税人编码");
            setting.Columns.Add("NSRSBH", "纳税人识别号");
            setting.Columns.Add("NSRMC", "企业名称");
            setting.Columns.Add("FAuditAmount", "补贴金额(万元)").CellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right; 
            setting.Columns.Add("FYear", "补贴年度");
            setting.Columns.Add("FApplyDate", "补贴日期");
            setting.Columns.Add("FIssueID", "期号");
            setting.Columns.Add("FFileNo", "文号");
            setting.SettingsExport.Styles.Header.BackColor = System.Drawing.Color.White;
            setting.SettingsExport.Styles.Header.ForeColor = System.Drawing.Color.Black;
            setting.Columns["FAuditAmount"].FooterCellStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Right;
            setting.Columns.Add("FFinancePropName", "资金性质");
            //setting.Columns.Add("NeedAmount", "还需补助金额");
           // setting.Columns.Add("FNote", "备注");
            setting.SettingsBehavior.AllowFocusedRow = true;
            setting.SettingsLoadingPanel.Text = "数据读取中...";
            setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Count, "NSRBM").DisplayFormat = "总计:";
              setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAuditAmount").DisplayFormat = "￥{0}";
            setting.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAuditAmount").DisplayFormat = "合计:￥{0}";
            //setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAmount").DisplayFormat = "￥{0}";
            //setting.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "FAmount").DisplayFormat = "合计:￥{0}";

            //setting.TotalSummary.Add(DevExpress.Data.SummaryItemType.Sum, "NeedAmount").DisplayFormat = "￥{0}";
            //setting.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "NeedAmount").DisplayFormat = "合计:￥{0}";
            setting.Settings.ShowGroupPanel = true;
            setting.Settings.ShowFooter = true;
            setting.ClientSideEvents.BeginCallback = "function(s,e){OnBeginCallback(s,e)}";
            setting.ClientSideEvents.EndCallback = "function(){}";
            setting.SettingsBehavior.EnableCustomizationWindow = true;
            setting.DataBinding = (sender, e) =>
            {
                ASPxGridView grid = (ASPxGridView)sender;
                for (int i = 0; i < grid.AllColumns.Count; i++)
                {
                    if (grid.AllColumns[i].HeaderStyle.HorizontalAlign == System.Web.UI.WebControls.HorizontalAlign.NotSet)
                    {
                        grid.AllColumns[i].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                    }
                }
            };
            setting.PreRender = setting.BeforeGetCallbackResult = (sender, e) =>
            {
               

                MVCxGridView gridView = sender as MVCxGridView;
                MVCxGridViewState gridViewState = new MVCxGridViewState(gridView);
                OperateSession.SetToCache(gridViewState, "QueryAllowance");

               
            };
            return setting;
        }
       
        #endregion
    }
}

   
